<?php

namespace Biz\model\wiki;

use Medoo\Medoo;


class WikiRankingPublicPraise extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'wiki_ranking_public_praise';

    const RANKING_STATE_NORMAL = 0;//默认,待发榜
    const RANKING_STATE_SHOW = 1;//张榜中
    const RANKING_STATE_HIDE = 2;//未上榜
    const RANKING_STATE_DEL = 9;//删除

    const DEFAULT_TOP_LIMIT = 20;//默认取top20


    public static function getByWhere($where, $select = '*')
    {
        return self::get($select, $where);
    }

    public static function selectByWhere($where, $select = '*')
    {
        $list = self::select($select, $where);
        return $list;
    }

    public static function selectCountWhere($where)
    {
        $count = self::count('id', $where);
        return $count;
    }


    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return self::id();
    }


    public static function updateData($data, $where)
    {
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }


    public static function getShowTop($ranking_id, $rankingDay, $limit = self::DEFAULT_TOP_LIMIT)
    {
        return self::select("*", [
            'AND' => [
                'ranking_id' => $ranking_id,
                'ranking_state' => self::RANKING_STATE_SHOW,
                'ranking_day' => $rankingDay,
            ],
            'ORDER' => ['top_num' => 'ASC', 'score' => 'DESC'],
            'LIMIT' => $limit
        ]);
    }

    public static function getListByWhere($where, $field = "*")
    {
        return self::select($field, $where);
    }

    public static function getCount($map)
    {
        $total = self::select(['cnt' => Medoo::raw('count(distinct ranking_id)')], $map);
        return $total[0]['cnt'] ?: 0;
    }

    /**
     * 根据条件查询总数
     * @param $where
     * @return bool|int|mixed|string
     */
    public static function getCountWhere($where)
    {
        return self::count($where);
    }


    public static function getCountMaps($map)
    {
        return self::select(['cnt' => Medoo::raw("count(DISTINCT `ranking_id`)"), 'brand_id'], $map);
    }

    public static function getShowRankingIds($rankingDay)
    {
        $rankingIds = self::select([
            'ranking_id' => Medoo::raw('distinct ranking_id')
        ], [
            'ranking_state' => self::RANKING_STATE_SHOW,
            'ranking_day' => $rankingDay
        ]);
        $rankingIds = $rankingIds ? array_column($rankingIds, 'ranking_id') : [];
        return $rankingIds;
    }

}